package com.zw.healthy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zw.api.UserFeignClient;
import com.zw.commons.utlis.JwtHelper;
import com.zw.healthy.mapper.HealthClockMapper;
import com.zw.healthy.service.HealthClockService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zw.object.healthy.HealthClock;
import com.zw.object.result.Result;
import com.zw.object.system.SysUser;
import com.zw.object.vo.ClockInfoVo;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 打卡健康表 服务实现类
 * </p>
 *
 * @author zw
 * @since 2023-03-12
 */
@Service
public class HealthClockServiceImpl extends ServiceImpl<HealthClockMapper, HealthClock> implements HealthClockService {

    @Autowired
    private UserFeignClient userFeignClient;
    @Override
    public Result saveClock(HealthClock healthClock) {
        int insert = baseMapper.insert(healthClock);
        return insert > 0 ? Result.ok():Result.fail();
    }

    @Override
    public Result clockInfoList(ClockInfoVo vo, String token) {
        String username = JwtHelper.getUsername(token);
        SysUser user = userFeignClient.getUserByUsername(username);
        Page<HealthClock> page = new Page<>(vo.getPageNum(), vo.getPageSize());
        LambdaQueryWrapper<HealthClock> wrapper = new LambdaQueryWrapper<>();
        if(user!=null && "学生".equals(user.getDescription())){
            wrapper.eq(HealthClock::getUsername,username);
            wrapper.orderByAsc(HealthClock::getCreateTime);
            Page<HealthClock> clockPage = baseMapper.selectPage(page, wrapper);
            return Result.ok(clockPage);
        }
        wrapper.eq(vo.getHealthType() != null, HealthClock::getHealthType, vo.getHealthType());
        wrapper.eq(vo.getMiddleHigh() != null, HealthClock::getMiddleHigh, vo.getMiddleHigh());
        wrapper.eq(vo.getDiagnosis() != null, HealthClock::getDiagnosis, vo.getDiagnosis());
        wrapper.eq(vo.getReturnInfo() != null, HealthClock::getReturnInfo, vo.getReturnInfo());
        wrapper.eq(vo.getDeptId() != null, HealthClock::getDeptId, vo.getDeptId());
        wrapper.orderByDesc(HealthClock::getCreateTime);
        Page<HealthClock> pageList = baseMapper.selectPage(page, wrapper);
        return Result.ok(pageList);
    }
}
